package com.lanou3g.demo.base.controller;

import com.lanou3g.demo.base.service.IUserService;
import com.lanou3g.demo.entity.AdminInfo;
import com.lanou3g.demo.entity.ExAdminInfo;
import com.lanou3g.demo.validate.ModifyPwValidate;
import com.lanou3g.demo.validate.ModifyUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@RequestMapping("/user")
public class UserInfoController {

    @Autowired
    private IUserService user;

    @RequestMapping("/info")
    public String findInfoByCode(Model model, HttpSession session) {
        if (session.getAttribute("userInfo") != null) {
            AdminInfo admin = (AdminInfo) session.getAttribute("userInfo");
            List<ExAdminInfo> infoList = user.findInfoByCode(admin.getAdminCode());
            model.addAttribute("info", infoList);
            return "/user/user_info";
        } else return "/login";

    }

    @RequestMapping("/modify")
    public String modifyInfoByCode(@Validated({ModifyUserInfo.class}) AdminInfo admin, BindingResult result, Model model, HttpSession session) {
        if (result.hasErrors()) {
            model.addAttribute("result", result);
            return findInfoByCode(model, session);
        } else {
            AdminInfo newAdmin = new AdminInfo();
            newAdmin.setAdminCode(admin.getAdminCode());
            newAdmin.setName(admin.getName());
            newAdmin.setEmail(admin.getEmail());
            newAdmin.setTelephone(admin.getTelephone());
            newAdmin.setEnrolldate(admin.getEnrolldate());
            user.modifyInfoByCode(newAdmin);
            System.out.println(newAdmin);
            return findInfoByCode(model, session);
        }
    }

    @RequestMapping("/modify.pw")
    public String modifyPwByCode(@Validated({ModifyPwValidate.class}) AdminInfo admin, BindingResult result, Model model, HttpSession session) {
        if (session.getAttribute("userInfo") != null) {
            if (result.hasErrors()) {
                model.addAttribute("result", result);
                return "/user/user_modi_pwd";
            } else {
                String adminCode = admin.getAdminCode();
                List<ExAdminInfo> adminInfos = user.findInfoByCode(adminCode);
                for (ExAdminInfo adminInfo : adminInfos) {
                    if (adminInfo.getPassword().equals(admin.getPassword())) {
                        AdminInfo newAdmin = new AdminInfo();
                        newAdmin.setAdminCode(admin.getAdminCode());
                        newAdmin.setPassword(admin.getNewPassword());
                        user.modifyPwByCode(newAdmin);
                        System.out.println(newAdmin);
                        return findInfoByCode(model, session);
                    } else {
                        model.addAttribute("error", "旧密码输入有误,请检查后重新输入");
                        return "/user/user_modi_pwd";
                    }
                }
                return findInfoByCode(model, session);
            }
        } else return "/login";
    }
}
